#include <REGX52.H>
#include "I2C.h"
#define AT24C02_ADDRESS 0xA0
//从机地址 最后一位 0 写  1 读
//数据帧模块

/**
  * @brief  AT24C02 写入一个字节
  * @param  WordAddress 要写入字节的地址
	* @param  Data 要写入的数据
  * @retval 无
  */
void AT24C02_WriteByte(unsigned char WordAddress,Data)//字地址 数据
{
 I2C_Start();
 I2C_SendByte(AT24C02_ADDRESS);//发送从机地址
 I2C_ReceiveAck();//主机接收应答
 //if(Ack==0)P2=0x00; //0 应答  1无应答
 I2C_SendByte(WordAddress);
 I2C_ReceiveAck();
 I2C_SendByte(Data);
 I2C_ReceiveAck();
 I2C_Stop();
}


/**
  * @brief  AT24C02 读取一个字节
  * @param  WordAddress 要写入的字节的地址
  * @retval 读出的数据
  */
unsigned char AT24C02_ReadByte(unsigned char WordAddress)//读取字地址返回数据
{
 unsigned char Data;
 I2C_Start();
 I2C_SendByte(AT24C02_ADDRESS);//从机地址写
 I2C_ReceiveAck();
 I2C_SendByte(WordAddress);
 I2C_ReceiveAck();
 I2C_Start();
 I2C_SendByte(AT24C02_ADDRESS|0x01);//从机地址写
 I2C_ReceiveAck();
 Data=I2C_ReceiveByte();
 I2C_SendAck(1);//主机相应应答1
 I2C_Stop();
 return Data;
}